package com.longrise.android.byjk.plugins.course.mycourse;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.android.byjk.event.CourseDetailEvent;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.event.TabLineShowEvent;
import com.longrise.android.byjk.model.AllCourseParentBean;
import com.longrise.common.base.BaseActivity2;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/9/22.
 * 我的课程
 */

public class MyCourseActivity extends BaseActivity2<MyCoursePresenter> implements MyCourseContract.View {
    private TabLayout mTb;
    private ViewPager mVp;
    private List<AllCourseParentBean> mParentBeanList = new ArrayList<>();
    private View mTabLine;
    private RelativeLayout mRltabbg;
    private TextView mTvEmpty;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.by_activity_mycourse;
    }

    @Override
    public void initView() {
        mTb = (TabLayout) findViewById(R.id.mycourse_tablayout);
        mVp = (ViewPager) findViewById(R.id.mycourse_viewpager);
        mTabLine = findViewById(R.id.mycourse_tab_line);
        mRltabbg = (RelativeLayout) findViewById(R.id.mycourse_tab_rl);
        mToolbar.setTitle("我的课程");
        mTvEmpty = (TextView) findViewById(R.id.mycourse_empty_tv);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    @Override
    public void reload() {
        super.reload();
        mPresenter.init();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void toEventResult(CourseDetailEvent event) {
        if (event == null) {
            return;
        }
        if (event.isUpdateCoursePraxis() || event.isCourseBuyStatus() || event.isRefreshDetail() || event.getCoursePosition() != null) {
            mPresenter.getData();
        }
    }

    //加载完成数据了再显示tab
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void toTabLineShowEvent(TabLineShowEvent event) {
        mTabLine.setVisibility(View.VISIBLE);
        mRltabbg.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initAdapter(List<AllCourseParentBean> parentList) {
        mParentBeanList.clear();
        mParentBeanList.addAll(parentList);
        MyCourseFragmentPagerAdapter pagerAdapter = new MyCourseFragmentPagerAdapter(getSupportFragmentManager());
        pagerAdapter.setData(mParentBeanList);
        mVp.setAdapter(pagerAdapter);
        mTb.setupWithViewPager(mVp);
    }

    @Override
    public void showEmptyPage() {
        mTvEmpty.setVisibility(View.VISIBLE);
        mRltabbg.setVisibility(View.GONE);
        mVp.setVisibility(View.GONE);
    }

}
